<?php
	session_start();
	if($_GET['_t']=='loginout'){session_destroy();header("Location: ".$_COOKIE['_url']);exit;}
	$_SESSION['userinfo']['email']?$_->users->_update(array('status'=>1),array('email'=>$_SESSION['userinfo']['email'])):"";
	session_unset();
	
	if($_POST)
	{
		switch($_POST['usertype'])
		{
			case 'email':
				$uinfo = $_->users->_select('*',array('email'=>$_POST['username'],'password'=>md5($_POST['password']),'status !'=>'2'));
			break;
			case 'phone':
				$uinfo = $_->users->_select('*',array('phone'=>$_POST['username'],'password'=>md5($_POST['password']),'status !'=>'2'));
			break;
		}
		if($uinfo = $uinfo[0])
		{
			$uinfo = $_->users->_update(array('status'=>0),array('email'=>$uinfo['email']))? $_->users->_select('*',array('email'=>$uinfo['email'])):"";
			$uinfo = $uinfo[0];
			if($_POST['remember'] == 1)
			{
				cookie(array('username'=>$_POST['username'],'usertype'=>$_POST['usertype']),3600*24*365);
			}else{
				cookie(array('username'=>$_POST['username'],'usertype'=>$_POST['usertype']),0);
			}

			cookie(array('_time'=>time(),'_sid'=>$_COOKIE['PHPSESSID']),$time = 3600);
			$_SESSION['userinfo'] = $uinfo;

			switch($uinfo['level'])
			{
				case 0:
					$location = '/admins/';//管理员登录到管理界面
					break;
				default:
					$location = $_COOKIE['_url'] ? $_COOKIE['_url'] : 'http://my.pic.dev/';//普通会员登录到个人中心
					break;
			}
			header("Location: $location");exit;
		}else{
			_redirect("通行验证失败！",'/login.php');exit;
		}
	}

	$usertype = $_COOKIE['usertype'];
	$username = $_COOKIE['username'];
	$smarty->_assign(array('username','usertype'));
	$smarty->_display();
?>